package team.stride.database.crud.users;

import java.util.ArrayList;
import java.util.List;

import team.stride.R;
import team.stride.database.StrideDB;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;

public class BDUsersActivity extends Activity
{
	private List<UsersItem> recordsList;
	private ListView listLV;
	private UsersAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bd_users_layout);

		listLV = (ListView) findViewById(R.id.usersListView);

		recordsList = new ArrayList<UsersItem>();
		adapter = new UsersAdapter(this, recordsList);
		listLV.setAdapter(adapter);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		recordsList.clear();
		Cursor cursor = managedQuery(StrideDB.CONTENT_URI_USERS, null, null, null, null);
		while (cursor.moveToNext())
		{
			final int id_index = cursor.getColumnIndex(StrideDB.ID_U);
			final int name_index = cursor.getColumnIndex(StrideDB.NAME_U);
			final int network_index = cursor.getColumnIndex(StrideDB.NETWORK_U);
			final int playlist_index = cursor.getColumnIndex(StrideDB.PLAYLIST_U);
			final int details_index = cursor.getColumnIndex(StrideDB.DETAILS_U);

			int id = cursor.getInt(id_index);
			String name = cursor.getString(name_index);
			String playlist = cursor.getString(playlist_index);
			String network = cursor.getString(network_index);
			String details = cursor.getString(details_index);
			recordsList.add(new UsersItem(id, name, network, playlist, details));
		}
		cursor.close();
		adapter.notifyDataSetChanged();
	}
}
